package com.aliyun.oss.userController;

import com.aliyun.oss.UserService.DishService;
import com.aliyun.oss.pojo.Vo.ResultVO;
import com.aliyun.oss.pojo.Vo.UserDish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/list")
    @Cacheable(cacheNames = "setmealCache",key = "#categoryId") //key：setmealCache::categoryId
    public ResultVO list(Long categoryId) {
//        构造redis中的key,规则:dish_分类id
//        String key="dish_"+categoryId;
        List<UserDish> userDishList=null;
//        查询redis中是否存在菜品数据
//        userDishList  = (List<UserDish>) redisTemplate.opsForValue().get(key);
//        if(userDishList!=null&&userDishList.size()>=0)
//            return ResultVO.success(userDishList);
//        如果存在，直接返回，无须查询数据库

//        如果不存在，查询数据库，将查询到的数据存储到redis中

        userDishList = dishService.list(categoryId);
//        redisTemplate.opsForValue().set(key,userDishList);
        return ResultVO.success(userDishList);
    }
}
